import 'dart:convert';
import 'dart:core';

import 'package:flutter_ahj_app/base/basewidget/HttpUtils/HttpBaseConst.dart';
import 'package:flutter_ahj_app/base/bean/show_files_bean.dart';
import 'package:flutter_ahj_app/base/utils/HttpUtils.dart';
import 'package:flutter_ahj_app/base/utils/oss/OSSUrlConsts.dart';

typedef void CallbackFilePath(String fileUrl);

class FilesPath {
  /// 获取 文件的路径
  ///  1/009dc9a3b3154b44af10900f52aa1ba0.jpg
  /// String filesName; ///文件名字
  /// String type;///区分是否是视频  是否是视频1:是 0:不是
  /// String style;///视频第一帧的图片样式 video/snapshot,t_1000,f_jpg,w_800,h_600,m_fast

  static getFilePath(String filesName, CallbackFilePath callbackFilePath) {
    if (filesName.startsWith("https://") || filesName.startsWith("http://")) {
      callbackFilePath(filesName);
    }else if(filesName.startsWith("public")){
      callbackFilePath(OSSUrlConsts.URL_UPLOAD_IMAGE_OSS+"/"+filesName);
    } else {
      var params = {"a": "a"};

      ///获取filesName 后缀
      String type;
      if (filesName.contains(".")) {
        List filesNameList = filesName.split(".");
        String suffixStr = filesNameList[1];

        suffixStr = suffixStr.toLowerCase(); //转换小写

//        if (suffixStr == "jpg" ||
//            suffixStr == "png" ||
//            suffixStr == "jpeg" ||
//            suffixStr == "bmp" ||
//            suffixStr == "tiff") {
//          type = "0";
//        } else if (suffixStr == "mp4") {
//          type = "1";
//        } else {
//          assert(type != null, "不支持该格式");
//          callbackFilePath("不支持该格式");
//          return;
//        }

        ///只区分 视频 非视频
        if (suffixStr == "mp4") {
          type = "1";
        } else {
          type = "0";
        }

        String url;
        if (type == "0") {
          ///不是视频
          url = HttpBaseConst.serverUrl +
              HttpBaseConst.downloadPath +
              "?" +
              "fileName=" +
              filesName +
              "&type=" +
              type;
        } else if (type == "1") {
          ///视频
          url = HttpBaseConst.serverUrl +
              HttpBaseConst.downloadPath +
              "?" +
              "fileName=" +
              filesName +
              "&style=video/snapshot,t_1000,f_jpg,w_800,h_600,m_fast" +
              "&type=" +
              type;
        }
        HttpUtils.httpGet(url, params, (result) {
          print(result);
          ShowFilesBean showFilesBean =
              ShowFilesBean.fromJson(json.decode(result));
          print("我是fileUrl" + showFilesBean.data.fileUrl);
          callbackFilePath(showFilesBean.data.fileUrl);
        });
      } else {
        // callbackFilePath("文件名格式不正确");
        callbackFilePath(null);
        assert(type != null, null);
      }
    }
  }
}
